package uutalk.netcloud.talk.base.util.thirdpart;

import android.content.Context;

import net.orange_box.storebox.StoreBox;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import uutalk.netcloud.talk.app.BootstrapApp;
import uutalk.netcloud.talk.model.bean.LoginInfo;
import uutalk.netcloud.talk.model.bean.UserInfo;
import uutalk.netcloud.talk.store.UserInfoPreference;

/**
 * Created by hujie on 16/6/19.
 * <p>
 * 用户管理的类
 */
public class UserManager {

    private boolean hasLogin = false;
    private UserInfoPreference preference;
    private UserInfo userInfo;
    @Inject
    public Context context;
    private static UserManager userManager;


    public static UserManager getInstance() {
        if (userManager == null) {
            userManager = new UserManager();
        }
        return userManager;
    }

    private UserManager() {
        BootstrapApp.get().appComponent().inject(this);
        createUserInfoPreference();
    }

    private void createUserInfoPreference() {
        if (preference == null) {
            preference = StoreBox.create(context, UserInfoPreference.class);
        }
    }


    public void saveLoginInfoToLocal(LoginInfo LoginInfo) {
        preference.setLoginInfo(LoginInfo);
    }

    public LoginInfo getLoginInfo() {
        return preference.getLoginInfo();
    }


    public void updateUserInfo(UserInfo userInfo) {
        //更新
        hasLogin = true;
        this.userInfo = userInfo;
        if (preference != null) {
            preference.setUserInfo(userInfo);
        }
        EventBus.getDefault().post(userInfo);
    }


    public UserInfo getUserInfo() {
        if (userInfo != null) {
            return userInfo;
        } else if (preference != null) {
            userInfo = preference.getUserInfo();
            return userInfo;
        }
        return null;
    }

    /**
     * 保存登录账号 和登录信息
     * @param userInfo
     */
    public void saveUserInfoToLocal(UserInfo userInfo) {
        updateUserInfo(userInfo);
    }


    /**
     * 清除全部登录信息
     */
    public void clear() {
        if (preference != null) {
            preference.clear();
        }
        destory();
    }


    /**
     * 只清除用户信息
     */
    public void clearUserInfo() {
        if (preference != null) {
            preference.removeUserInfo();
        }
        destory();
    }

    /**
     * 清除登录信息
     */
    public void clearLoginInfo(){
        if(preference != null){
            preference.removeLoginInfo();
        }
    }

    public void destory() {
        hasLogin = false;
        if (preference != null) {
            preference = null;
            userManager = null;
            userInfo = null;
        }
    }

    /**
     * 检查是否需要重新登录
     */
    public boolean checkNeedReLogin() {

        return true;

    }

    /**
     * 异步登录下
     */
    public void asyncLogin() {

        if (!hasLogin) {
            if (getUserInfo() != null) {
            }
        }
    }

    public boolean isHasLogin() {
        return hasLogin;
    }
}
